<?php

namespace App\Http\Controllers;

use App\Models\About;
use App\Models\Article;
use App\Models\ArticleType;
use App\Models\Comment;
use App\Models\Home;
use App\Models\Picture;
use App\Models\SiteInfo;
use App\Models\Story;
use App\Models\Test;
use App\Models\TestResult;
use App\Models\User;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function index(){
        if(isset($_GET['page'])){
            $currentPage=$_GET['page'];
        }else{
            $currentPage=1;
        }
        $homes=Home::orderBy('created_at', 'desc')->paginate(10);
        $length=ceil(count(Home::all())/10);
        $is_mobile=$this->is_mobile();
        if($is_mobile){
            return view('mobile/index')->with('nav_num',0)->with('homes',$homes)->with('page_num',$length)->with('currentPage',$currentPage);
        }else{
            return view('index/index')->with('nav_num',0)->with('homes',$homes)->with('page_num',$length)->with('currentPage',$currentPage);
        }
    }

    public function picture_index(){
        if(isset($_GET['page'])){
            $currentPage=$_GET['page'];
        }else{
            $currentPage=1;
        }
        $is_mobile=$this->is_mobile();
        $pictures=Picture::orderBy('created_at', 'desc')->paginate(10);
        $length=ceil(count(Picture::all())/10);
        if($is_mobile){
            return view('mobile/pictureIndex')->with('pictures',$pictures)->with('nav_num',1)->with('page_num',$length)->with('currentPage',$currentPage);
        }else{
            return view('index/pictureIndex')->with('pictures',$pictures)->with('nav_num',1)->with('page_num',$length)->with('currentPage',$currentPage);
        }
    }

    public function test_index(){
        if(isset($_GET['page'])){
            $currentPage=$_GET['page'];
        }else{
            $currentPage=1;
        }
        $is_mobile=$this->is_mobile();
        $tests=Test::orderBy('created_at', 'desc')->paginate(10);
        $length=ceil(count(Test::all())/10);
        if($is_mobile){
            return view('mobile/testIndex')->with('tests',$tests)->with('nav_num',3)->with('page_num',$length)->with('currentPage',$currentPage);
        }else{
            return view('index/testIndex')->with('tests',$tests)->with('nav_num',3)->with('page_num',$length)->with('currentPage',$currentPage);
        }
    }

/*    public function articleDetail($articleId){
        $article=Article::find($articleId);
        $article->browse=$article->browse+1;
        $article->save();
        $is_mobile=$this->is_mobile();
        $article_comments=DB::table('comment')
            ->where(['articleId'=>$article->id,'comment.type'=>0])
            ->join('user',function($join)
            {
                $join->on('comment.userId', '=', 'user.id');
            })->select('comment.id','user.name','comment.articleId','comment.content','user.avatar')
            ->get();
        //获取上一条和下一条
        $next = Article::where('id', '<', $articleId)->max('id');
        $before = Article::where('id', '>', $articleId)->min('id');
        if($is_mobile){
            return view('mobile/articleDetail')->with('article',$article)->with('nav_num',0)->with('article_comments',$article_comments);
        }else{
            return view('index/articleDetail')->with('article',$article)->with('nav_num',0)->with('article_comments',$article_comments);
        }
    }*/

    public function pictureDetail($pictureId){
        $picture=Picture::find($pictureId);
        $picture->browse=$picture->browse+1;
        $picture->save();

        $home=Home::where(['itemId'=>$pictureId,'typeId'=>1])->first();
        if($home != null){
            $home->browse=$home->browse+1;
            $home->save();
        }

        $is_mobile=$this->is_mobile();
        $picture_comments=DB::table('comment')
            ->where(['articleId'=>$picture->id,'comment.type'=>1])
            ->join('user',function($join)
            {
                $join->on('comment.userId', '=', 'user.id');
            })->select('comment.id','user.name','comment.articleId','comment.content','user.avatar','comment.created_at','comment.reply_name','comment.reply_content')
            ->get();
        //获取上一条和下一条
        $next = Picture::where('id', '<', $pictureId)->max('id');
        $before = Picture::where('id', '>', $pictureId)->min('id');
        if($is_mobile){
            return view('mobile/pictureDetail')->with('picture',$picture)->with('nav_num',1)->with('picture_comments',$picture_comments)->with('before',$before)->with('next',$next);
        }else{
            return view('index/pictureDetail')->with('picture',$picture)->with('nav_num',1)->with('picture_comments',$picture_comments)->with('before',$before)->with('next',$next);
        }
    }

    public function testDetail($testId){
        $test=Test::find($testId);
        $test->browse=$test->browse+1;
        $test->save();
        $is_mobile=$this->is_mobile();
        $testResult=TestResult::where('testId',$testId)->get();
        $length=count($testResult);
        $num=rand(0,$length-1);
        $ts=$testResult[$num];
        if($is_mobile){
            return view('mobile/testDetail')->with('test',$test)->with('nav_num',3)->with('testResult',$ts);
        }else{
            return view('index/testDetail')->with('test',$test)->with('nav_num',3)->with('testResult',$ts);
        }
    }

    public function doComment(){
        $comment=new Comment;
        $comment->userId=$_POST['userId'];
        $comment->articleId=$_POST['articleId'];
        $comment->content=$_POST['content'];
        $comment->type=$_POST['type'];
        $comment->save();

        return 'success';
    }

    public function doCommentByReply(){
        $comment=new Comment;
        $comment->userId=$_POST['userId'];
        $comment->articleId=$_POST['articleId'];
        $comment->content=$_POST['content'];
        $comment->type=$_POST['type'];
        $comment->reply_name=$_POST['reply_name'];
        $comment->reply_content=$_POST['reply_content'];
        $comment->save();

        return 'success';
    }

    public function getResult(){
        $testId=$_POST['testId'];
        $testResult=TestResult::where('testId',$testId)->get();
        $length=count($testResult);
        $num=rand(0,$length-1);
        $ts=$testResult[$num];
        echo json_encode($ts);
    }

    public function story_index(){
        if(isset($_GET['page'])){
            $currentPage=$_GET['page'];
        }else{
            $currentPage=1;
        }
        $is_mobile=$this->is_mobile();
        $articletypes=ArticleType::all();
        $articles=Article::orderBy('created_at', 'desc')->paginate(10);
        $length=ceil(count(Article::all())/10);
        if($is_mobile){
            return view('mobile/storyIndex')->with('articles',$articles)->with('nav_num',2)->with('page_num',$length)->with('currentPage',$currentPage)->with('articletypes',$articletypes)->with('page_url','/story');
        }else{
            return view('index/storyIndex')->with('articles',$articles)->with('nav_num',2)->with('page_num',$length)->with('currentPage',$currentPage)->with('articletypes',$articletypes)->with('page_url','/story');
        }
    }

    public function storyDetail($storyId){
        $article=Article::find($storyId);
        $article->browse=$article->browse+1;
        $article->save();

        $home=Home::where(['itemId'=>$storyId,'typeId'=>0])->first();
        if($home!=null){
            $home->browse=$home->browse+1;
            $home->save();
        }

        $is_mobile=$this->is_mobile();
        $article_comments=DB::table('comment')
            ->where(['articleId'=>$article->id,'comment.type'=>0])
            ->join('user',function($join)
            {
                $join->on('comment.userId', '=', 'user.id');
            })->select('comment.id','user.name','comment.articleId','comment.content','user.avatar','comment.created_at','comment.reply_name','comment.reply_content')
            ->get();
        //获取上一条和下一条
        $next = Article::where('id', '<', $storyId)->max('id');
        $before = Article::where('id', '>', $storyId)->min('id');
        if($is_mobile){
            return view('mobile/storyDetail')->with('article',$article)->with('nav_num',2)->with('article_comments',$article_comments)->with('before',$before)->with('next',$next);
        }else{
            return view('index/storyDetail')->with('article',$article)->with('nav_num',2)->with('article_comments',$article_comments)->with('before',$before)->with('next',$next);
        }
    }

    public function storyByType($typeId){
        if(isset($_GET['page'])){
            $currentPage=$_GET['page'];
        }else{
            $currentPage=1;
        }
        $is_mobile=$this->is_mobile();
        $articletypes=ArticleType::all();
        $articles=Article::where('typeId',$typeId)->paginate(10);
        $length=ceil(count(Article::where('typeId',$typeId)->get())/10);
        if($is_mobile){
            return view('mobile/storyIndex')->with('articles',$articles)->with('nav_num',2)->with('page_num',$length)->with('currentPage',$currentPage)->with('articletypes',$articletypes)->with('page_url','/story/'.$typeId);
        }else{
            return view('index/storyIndex')->with('articles',$articles)->with('nav_num',2)->with('page_num',$length)->with('currentPage',$currentPage)->with('articletypes',$articletypes)->with('page_url','/story/'.$typeId);
        }
    }

    public function about(){
        $is_mobile=$this->is_mobile();
        $about=SiteInfo::find(1);
        if($is_mobile){
            return view('mobile/aboutIndex')->with('about',$about)->with('nav_num',4);
        }else{
            return view('index/aboutIndex')->with('about',$about)->with('nav_num',4);
        }
    }

    //手机网页跳转
    //如果检测到访问的浏览器为下列一个指定的移动浏览器 则返回true
    public function is_mobile(){
        $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
        $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
        $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
        $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
        $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
        $regex_match.=")/i";
        return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
    }
}
